package com.syni.mdd.yhd.common.all.dao.jpa;

import com.syni.mdd.yhd.common.all.dao.BaseJPADao;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessLabel;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessTypeRelation;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface BmsBusinessLabelDao extends BaseJPADao<BmsBusinessLabel,Integer> {
    List<BmsBusinessLabel> findByIdInAndIsDelete(Set<Integer> ids,Integer isDetele);

    @Query(value = "select bbl.id,bbl.label_name as labelName,bbl.label_name as label_name from bms_business_label bbl where bbl.label_name like %?2% " +
            "and bbl.is_delete = ?3 and bbl.type_id = ?1",nativeQuery = true)
    List<Map<String,Object>> findByTypeIdAndLabelNameLikeAndIsDelete(Integer typeId,String labelName,Integer isDelete,Pageable pageable);

    @Query(value = "select bbl.id,bbl.label_name as labelName,bbl.label_name as label_name from bms_business_label bbl where bbl.label_name like %?1% " +
            "and bbl.is_delete = ?2 ",nativeQuery = true)
    List<Map<String,Object>> findByLabelNameLikeAndIsDelete(String labelName,Integer isDelete,Pageable pageable);


    List<BmsBusinessLabel> findByLabelNameInAndIsDelete(Set labels,Integer isDelete);

    List<BmsBusinessLabel> findByTypeIdInAndIsDelete(Set typeIds,Integer isDelete);

    @Query(value = "select bbl.* from bms_dx_group_buy bb " +
            "left join bms_business bu on bb.business_id = bu.id " +
            "LEFT JOIN bms_business_label bbl on bu.label = bbl.label_name " +
            "LEFT JOIN bms_business_type_relation bbtr on bb.business_id = bbtr.bms_business_id " +
            "where bb.status = 1 and bbl.id is not null and IF(?2 != '',bu.addr_city=?2 and IF(?3 != '',bu.addr_district=?3,1=1),1=1) and bbtr.bms_type_id in (?1) group by bbl.id",nativeQuery = true)
    List<BmsBusinessLabel> selfFindByTypeIds(Set<Integer> typeIds,String addrCity,String addrDistrict);

    List<BmsBusinessLabel> findByIdIn(Set<Integer> labelIds);

    List<BmsBusinessLabel> findByLabelNameIn(Set<String> labels);
}
